
#include "as_strided_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"
#include <cstdint>

namespace optiling
{
static ge::graphStatus TilingFunc(gert::TilingContext *context)
{

    AsStridedTilingData tiling;
    context->SetBlockDim(1);
    auto x1_shape = context->GetInputShape(0)->GetStorageShape();
    int64_t xShape[64] = {0};
    xShape[0] = x1_shape.GetDimNum();
    shape_for(x1_shape, [&](int64_t i, int64_t shapei) { xShape[i + 2] = shapei; });
    tiling.set_xShape(xShape);

    auto size_shape = context->GetInputShape(1)->GetStorageShape();
    int64_t sizeShape[64] = {0};
    sizeShape[0] = size_shape.GetDimNum();
    shape_for(size_shape, [&](int64_t i, int64_t shapei) { sizeShape[i + 2] = shapei; });
    tiling.set_sizeStrideShape(sizeShape);

    tiling_zero_workspace(context);
    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());

    print_shape_vector("xShape", xShape);
    print_shape_vector("sizeStrideShape", sizeShape);
    
    return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge
{
static ge::graphStatus InferShape(gert::InferShapeContext *context)
{
    const gert::Shape *x1_shape = context->GetInputShape(0);
    gert::Shape *y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops
{
class AsStrided : public OpDef
{
  public:
    explicit AsStrided(const char *name) : OpDef(name)
    {
        this->Input("x")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("size")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("stride")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("storage_offset")
            .ParamType(REQUIRED)
            .DataType({ge::DT_INT32, ge::DT_INT32, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore().SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");
    }
};

OP_ADD(AsStrided);
} // namespace ops
